import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect  // 声明为切面类
@Component  // 作为Spring的bean进行管理
public class LogAspect {

    // 定义切点：所有被@Service注解标注的方法
    @Pointcut("execution(* com.example.demo.service.*.*(..))")
    public void serviceMethods() {}

    // 在目标方法执行前执行
    @Before("serviceMethods()")
    public void logBefore(JoinPoint joinPoint) {
        System.out.println("Before method execution: " + joinPoint.getSignature());
    }

    // 在目标方法执行后执行
    @After("serviceMethods()")
    public void logAfter(JoinPoint joinPoint) {
        System.out.println("After method execution: " + joinPoint.getSignature());
    }
}
